package com.nutrition.health.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "diet_records")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class DietRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @NotNull
    private LocalDateTime consumptionTime;

    @Enumerated(EnumType.STRING)
    private MealType mealType;

    @Column(length = 500)
    private String notes;

    @ManyToMany
    @JoinTable(
        name = "diet_record_food_items",
        joinColumns = @JoinColumn(name = "diet_record_id"),
        inverseJoinColumns = @JoinColumn(name = "food_item_id")
    )
    private Set<FoodItem> foodItems = new HashSet<>();

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;

    // 餐食类型枚举
    public enum MealType {
        BREAKFAST, // 早餐
        LUNCH, // 午餐
        DINNER, // 晚餐
        SNACK // 零食
    }
}